<?php

namespace app\common\model\xilumarket;

use think\Model;

class GroupChat extends Model
{
    protected $table = 'xl_xilumarket_group_chat';

    // 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
    protected $autoWriteTimestamp = 'int';
    // 创建时间字段
    protected $createTime = 'createtime';
    // 更新时间字段
    protected $updateTime = 'updatetime';

    protected $append = [
        'createtime_text',
    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? xilumarket_time_tran($value) : $value;
    }
    protected function getReceiveUserIdAttr(){
        return null;
    }

    public function session() {
        return $this->belongsTo(ChatSession::class, 'session_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function senduser() {
        return $this->belongsTo(User::class, 'send_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function read() {
        return $this->hasOne(GroupChatRead::class, 'group_chat_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
